package com.ruoyi.project.data.filedef.controller;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.data.filedef.domain.DsFileDef;
import com.ruoyi.project.data.filedef.service.IDsFileDefService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import java.util.Arrays;

import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 数据文件定义Controller
 *
 * @author 魏刚
 * @date 2020-03-15
 */
@Controller
@RequestMapping("/data/filedef")
public class DsFileDefController extends BaseController
{
    private String prefix = "data/filedef";

    @Autowired
    private IDsFileDefService dsFileDefService;

    @RequiresPermissions("data:filedef:view")
    @GetMapping()
    public String filedef()
    {
        return prefix + "/filedef";
    }

    /**
     * 查询数据文件定义列表
     */
    @RequiresPermissions("data:filedef:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DsFileDef dsFileDef)
    {
        startPage();
        List<DsFileDef> list = dsFileDefService.list(new QueryWrapper<>(dsFileDef).orderByAsc("source_id"));
        return getDataTable(list);
    }

    /**
     * 导出数据文件定义列表
     */
    @RequiresPermissions("data:filedef:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DsFileDef dsFileDef)
    {
        List<DsFileDef> list = dsFileDefService.list(new QueryWrapper<>(dsFileDef).orderByAsc("source_id"));
        ExcelUtil<DsFileDef> util = new ExcelUtil<DsFileDef>(DsFileDef.class);
        return util.exportExcel(list, "filedef");
    }

    /**
     * 新增数据文件定义
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存数据文件定义
     */
    @RequiresPermissions("data:filedef:add")
    @Log(title = "数据文件定义", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DsFileDef dsFileDef)
    {
        return toAjax(dsFileDefService.insertDsFileDef(dsFileDef));
    }

    /**
     * 修改数据文件定义
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        DsFileDef dsFileDef = dsFileDefService.getById(id);
        mmap.put("dsFileDef", dsFileDef);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据文件定义
     */
    @RequiresPermissions("data:filedef:edit")
    @Log(title = "数据文件定义", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DsFileDef dsFileDef)
    {
        return toAjax(dsFileDefService.saveOrUpdate(dsFileDef));
    }

    /**
     * 删除数据文件定义
     */
    @RequiresPermissions("data:filedef:remove")
    @Log(title = "数据文件定义", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dsFileDefService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
